<?php

  $timetag = dechex(time());
  $workswapfile = WORKSWAP_LOG_FILE;
  $fh = fopen($workswapfile,"a");
	//debugging..
  ob_start();
?>
<div style="font-size:18px; border-top: 2px solid black; ">Tagged: <?= $timetag ?></div>
<div><?= date("r"); ?></div>
<div style="border:thin solid #000000; padding: 10px; margin: 10px;">
	<div style="font-size:16px; font-weight:bold; ">Create temporary table</div>
	<?php
		$sql = "CREATE temporary table workswap_data (wsid int, shift_date int, shift_start int, shift_end int)";
		$result = mysql_query($sql);
	?>
	<div style="border:thin solid #000066; background-color:#AAAAFF; padding: 15px; margin: 10px; font-size:12px;">SQL: <?= $sql ?></div>
</div>
<div style="border:thin solid #000000; padding: 10px; margin: 10px;">
	<div style="font-size:16px; font-weight:bold; ">Insert temporary shifts</div>
	<?php
	$sql = "Insert into workswap_data (wsid,shift_date,shift_start,shift_end) select workswap_id,shift_date,start,end from t_workswap where `new_driver` IS NULL and shift_date > " . date("Ymd");
	$result = mysql_query($sql);
	?>
	<div style="border:thin solid #000066; background-color:#AAAAFF; padding: 15px; margin: 10px; font-size:12px;">SQL: <?= $sql ?></div>
</div>
<div style="border:thin solid #000000; padding: 10px; margin: 10px;">
	<div style="font-size:16px; font-weight:bold; ">Insert permanent shifts</div>
	<?php
	$sql = "Insert into workswap_data (wsid,shift_date,shift_start) select t_workswap.workswap_id, t_sched_dates.date_id, t_workswap.start  from t_workswap, t_sched_shifts, t_sched_structure, t_sched_dates
			where `t_workswap`.`new_driver` IS NULL and
  			(t_workswap.shift_date is null and t_workswap.shift_id = t_sched_shifts.shift_id and t_sched_shifts.structure_id = t_sched_structure.structure_id and t_sched_structure.table_id = t_sched_dates.table_id and t_sched_dates.date_id > " . date("Ymd") . ")";
	$result = mysql_query($sql);
	?>
	<div style="border:thin solid #000066; background-color:#AAAAFF; padding: 15px; margin: 10px; font-size:12px;">SQL: <?= $sql ?></div>
</div>
<div style="border:thin solid #000000; padding: 10px; margin: 10px; font-size:10px;">
	<div style="font-size:16px; font-weight:bold; ">Update <i>NULL</i> Start Times</div>
	<?php
	$sql = "SELECT * FROM `workswap_data` WHERE `shift_start` IS NULL";
	$result = mysql_query($sql);
	?>
	<div style="border:thin solid #000066; background-color:#AAAAFF; padding: 15px; margin: 10px; font-size:12px;">SQL: <?= $sql ?></div>
	<?php
	if(mysql_num_rows($result) > 0) {
		while($data = mysql_fetch_assoc($result)) {
			$sql_select = sprintf("SELECT `t_sched_shifts`.`shift_start`,`t_sched_shifts`.`shift_end` FROM `t_sched_shifts`,`t_workswap` WHERE `t_workswap`.`workswap_id` = %s AND `t_workswap`.`shift_id` = `t_sched_shifts`.`shift_id`",$data['wsid']);
			$result_select = mysql_query($sql_select);
			$data_select = mysql_fetch_assoc($result_select);
			$sql_update = sprintf("UPDATE `workswap_data` SET `shift_start` = %s, `shift_end` = %s WHERE `wsid` = %s AND `shift_date` = %s",$data_select['shift_start'],$data_select['shift_end'],$data['wsid'],$data['shift_date']);
			$result_update = mysql_query($sql_update);
			if(mysql_affected_rows() == 1) {
				$color = 'green';
			} else {
				$color = 'red';
				$error = 'MySQL said: ' . mysql_errno() . ' - ' . mysql_error();
				print "<span style='font-weight: bold; color: red;'>" . $error . "</span><br />\n";
			}
		print "INSERT: <span style='color: " . $color . ";'>" . $sql_update . "</span><br />\n";
		}
	}
	?>
</div>

<div style="margin:0px; padding:5px; background-color:#000000; color:#FFFFFF; font-weight:bold; text-align:center;">Test Run - WS Board</div>
<?php 

	$debug_data = ob_get_contents();
	ob_end_clean();
	fwrite($fh,$debug_data);
	ob_start();
?>


<div style=" text-align:center; ">
<?php
	$sql = "SELECT * 
			FROM `workswap_data`,`t_workswap`
			WHERE `workswap_data`.`wsid` = `t_workswap`.`workswap_id`
			ORDER BY `workswap_data`.`shift_date`, `workswap_data`.`shift_start`";
	$today = date("Ymd");
	$sql = "SELECT `workswap_data`.*,`t_sched_structure`.`description`, `t_workswap`.`shift_date` permanent, `t_workswap`.`redstar`, `t_workswap`.`orig_driver`
						FROM `t_workswap`, `t_sched_shifts`, `t_drivers`, `t_sched_structure`, `workswap_data`
						WHERE `workswap_data`.`wsid` = `t_workswap`.`workswap_id`
						AND `t_sched_structure`.`structure_id` = `t_sched_shifts`.`structure_id`
						AND `t_drivers`.`driver_id` = `t_sched_shifts`.`driver_id`
						AND `t_sched_shifts`.`shift_id` = `t_workswap`.`shift_id`
						ORDER BY `workswap_data`.`shift_date`, `workswap_data`.`shift_start`";

	$result = mysql_query($sql);
	if($result === false) {
		die("Error with SQL: MySQL said: " . mysql_errno() . " " . mysql_error());
	}	
	if(mysql_num_rows($result) > 0) {
		$previousDate = -1;
		print "<div style='align:center;'><table style='width: 400px; margin:auto;'>\n";
		while($data = mysql_fetch_assoc($result)) {
			if($data['shift_date'] != $previousDate)
				print "<tr><td colspan=3 style='padding-top: 10px; font-weight:bold; text-align: left; border-bottom: 1px solid black;'>" . formatDate($data['shift_date']) . "</td></tr>\n";
			$previousDate = $data['shift_date'];
			if($data['redstar'] == 0) {
				$color = '';
			} else {
				$color = 'background-color:#FFE1E1;';
			}
			if(!$data['permanent']) {
				$links = "<td style=\"$color; text-align:center;\"><a href=\"javascript:confirmQuestion('index.php?workswap=take&wsid=" . $data['wsid'] . "','Are you sure you want to take this shift?')\">Permanent</a></td><td style=\"$color; text-align:center;\">&nbsp;</td>";
			} else {
				$links = "<td style=\"$color; text-align:center;\">&nbsp;</td><td style=\"$color; text-align:center;\"><a href=\"javascript:confirmQuestion('index.php?workswap=take&wsid=" . $data['wsid'] . "','Are you sure you want to take this shift?')\">Temporary</a></td>";
			}
			if($data['orig_driver'] == $_SESSION['driver_id']) {
				$links = "<td colspan=2 style=\"$color; text-align:center;\">( <a href=\"?workswap=cancel&wsid=" . $data['wsid'] . "\">Cancel Swap</a> )</td>";
			}
			print "<tr>
					<td style=\"padding-left: 10px; $color;\">" . $data['description'] . " " . $data['shift_start'] . "-" . $data['shift_end'] ."</td>" . $links . "</tr>\n";
		}
		print "</table></div>";
	} else {
		print "No data";
	}
?>
</div>
<div style="text-align:right; font-size:10px; font-weight:bold; ">Debug Tag: <?= $timetag ?></div>
<?php

	$workswap_board = ob_get_contents();
	fwrite($fh,$workswap_board);	
	ob_end_flush();
	
?>